//    Copyright (c) 2009 Elwyn Malethan
//
//    This file is part of SeemoreJ.
//
//    SeemoreJ is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SeemoreJ is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with SeemoreJ.  If not, see <http://www.gnu.org/licenses/>.

package com.malethan.seemorej.taglib.elements;

import com.malethan.seemorej.taglib.Util;

import java.util.Collection;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Select extends BaseElement {
    //----------------------------------------------------------------------- Static Properties and Constants

    private static final Log log = LogFactory.getLog(Select.class);

    //----------------------------------------------------------------------- Static Methods

    protected static String getProperty(Object bean, String property) {
        try {
            Method getter = bean.getClass().getMethod("get" + property.substring(0,1).toUpperCase() + property.substring(1));
            return String.valueOf(getter.invoke(bean));
        } catch (Exception e) {
            log.error(e);
        }
        return null;
    }

    //----------------------------------------------------------------------- Instance Properties

    Collection options;
    String optionLabel = "label";
    String optionValue = "value"; 

    //----------------------------------------------------------------------- Constructors

    public Select() {
        super("select");
    }

    //----------------------------------------------------------------------- Getters and Setters

    public Collection getOptions() {
        return options;
    }

    public void setOptions(Collection options) {
        this.options = options;
    }

    public String getOptionLabel() {
        return optionLabel;
    }

    public void setOptionLabel(String optionLabel) {
        this.optionLabel = optionLabel;
    }

    public String getOptionValue() {
        return optionValue;
    }

    public void setOptionValue(String optionValue) {
        this.optionValue = optionValue;
    }

    //----------------------------------------------------------------------- Instance Methods

    /**
     * Implementations should return the HTML for the element
     *
     * @return a string representation of the object.
     */
    public String toString() {
        StringBuffer html = new StringBuffer("<");
        html.append(elementName);
        appendAttributes(html, this);
        html.append(">");

        if (options != null) {
            for (Object option : options) {
                html.append("<option");
                String optVal = getProperty(option, optionValue);
                Util.appendAttribute(html, "value", optVal);
                if(optVal != null && optVal.equals(value)) {
                    Util.appendAttribute(html, "selected", "selected");
                }
                html.append(">");
                html.append(getProperty(option, optionLabel));
                html.append("</option>");
            }
        }

        html.append("</");
        html.append(elementName);
        html.append(">");

        return html.toString();
    }

}
